{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9cc62bc7-5408-4dfe-a9e5-cc773a1595ed",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-23T10:06:06.583433Z",
     "iopub.status.busy": "2024-11-23T10:06:06.583022Z",
     "iopub.status.idle": "2024-11-23T10:06:06.590218Z",
     "shell.execute_reply": "2024-11-23T10:06:06.588478Z",
     "shell.execute_reply.started": "2024-11-23T10:06:06.583410Z"
    }
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "import einops\n",
    "from einops import repeat"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b9275d2-1c60-4914-8701-df5497814309",
   "metadata": {},
   "source": [
    "#### repeat"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22bfd1db-1cb7-4a41-be90-15e7986d49da",
   "metadata": {},
   "source": [
    "- 模式 \"i -> (i j)\" 的含义：\n",
    "    - 输入维度：i，即原始的一维张量。\n",
    "    - 输出维度：(i j)，表示将每个元素 i 重复 j 次后展平成一个新的维度。\n",
    "- 参数 j=p 指定每个元素重复的次数为 p。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "59b0e5ad-dffa-4831-bbfa-8a14c971c899",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-23T10:06:39.266087Z",
     "iopub.status.busy": "2024-11-23T10:06:39.265444Z",
     "iopub.status.idle": "2024-11-23T10:06:40.299609Z",
     "shell.execute_reply": "2024-11-23T10:06:40.297826Z",
     "shell.execute_reply.started": "2024-11-23T10:06:39.266042Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5,\n",
       "        5])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "repeat(torch.tensor([1, 2, 3, 4, 5]), 'i -> (i j)', j=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c16cdedd-eefd-47ba-a9dd-9be4fc2e09be",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-23T10:09:08.088125Z",
     "iopub.status.busy": "2024-11-23T10:09:08.087482Z",
     "iopub.status.idle": "2024-11-23T10:09:08.102022Z",
     "shell.execute_reply": "2024-11-23T10:09:08.099919Z",
     "shell.execute_reply.started": "2024-11-23T10:09:08.088080Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]), torch.Size([12]))"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p = repeat(torch.tensor(10), '-> (i j)', i=3, j=4)\n",
    "p, p.shape"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
